<div id="comment">

    <ul id="comment_list">
        <?php if (isset($comments) && $comments) : ?>
            <?php foreach ($comments as $v) : ?>
                <li>
                    <div class="author_avatar"><img src="<?php echo gravatar($v->email, 'G', 50); ?>" alt="avatar" /></div>
                    <div class="prefix_1">
                        <time datetime="<?php echo date($v->created); ?>" pubdate="pubdate"><?php echo date($v->created); ?></time>
                        <p><?php echo $v->content; ?></p>
                    </div>
                </li>
            <?php endforeach; ?>
        <?php endif; ?>
    </ul>
    <h2>Comment</h2>
    <form action="/comments/create" method="POST" class="post_form" id="comment_form">
        <ul>
            <li>
                <label for="email">邮箱 : </label>
                <input type="text" name="email" id="email" />
            </li>
            <li>
                <label for="content">评论 : </label>
                <textarea id="content" name="content"></textarea>
                <input type="hidden" name="did" value="<?php echo $did; ?>"/>
                <input type="hidden" name="dtype" value="<?php echo $dtype; ?>"/>
            </li>
            <li id="show_captcha"></li>
            <li><input type="submit" value="提交"/></li>
        </ul>
    </form>
    <script type="text/javascript" src="<?php echo base_url(); ?>js/Jquery/jquery.form.js"></script>
    <script>
        //把评论插入到列表中
        function showResponse(data)  { 
            if(data.result){
                $('#comment_list').append(data.message+'\n');
                $('#show_captcha').empty();
                $('#comment_form').get(0).reset();
            } else {
                $('#comment_form').prepend(data.message+'\n');
            }
            return false;
        }
        
        //插入验证码
        function insertCaptcha(data){
            $('#show_captcha').html(data);
        }
        
        //显示验证码
        function showCaptcha(){
            $.ajax({
                type : 'GET',
                url : '/captcha/create',
                success : insertCaptcha
            });
        }
        
        //重新转入验证码
        function reloadCaptcha(){
            showCaptcha();
            return false;
        }
        
        $('document').ready(function(){
            var options = {
                dataType : 'json',
                success : showResponse
            }
            $('#comment_form').ajaxForm(options); 
            
            $('#content').live('click',function(){
                var cap = $('#show_captcha').html();
                if(cap == ''){
                    showCaptcha();
                }
            });
            
            
        });
    </script>
</div>
